<template>
	<view>

		<u-toast ref="uToast"></u-toast><!-- 弹窗 -->
		<!-- :rules表单校验规则，通过rules属性来指定表单项的校验规则 ref:给表单设置一个引用名称 errorType设置表单错误提示方式
		label-position表单项的标签位置为左对齐 label-align设置标签文字对齐方式为左对齐-->
		<view class="form-container">
			<u-form :model="dataForm" :rules="rules" ref="dataForm" :errorType="['toast']" label-position="left"
				label-width="150" label-align="left">

				<!-- 抽样方案 -->
				<template>
					<view class="form-list">
						<!-- 抽样方案 @change="handleChange"-->
						<u-form-item v-show="param.sample == 'Y'" label="抽样方案" prop="samplingType" label-width="180" required class="form-item">
							<shzy-select v-model="dataForm.samplingType" placeholder="请选择"
								:options="samplingTypeList"></shzy-select>
						</u-form-item>

						<!-- 国标AQL -->
						<!-- 这里dataForm.samplingType拿到的是接口返回的encode,也就是字典项配置的编号,比如国标AQL的encode=1 -->
						<view v-show="dataForm.samplingType == '1'">

							<u-form-item label="检验水平" prop="gLevel" label-width="180" required class="form-item">
								<shzy-select v-model="dataForm.gLevel" placeholder="请选择"
									:options="gLevelList"></shzy-select>
							</u-form-item>

							<u-form-item label="国际严格度" prop="gStringency" label-width="180" required class="form-item">
								<shzy-select v-model="dataForm.gStringency" placeholder="请选择"
									:options="gStringencyList"></shzy-select>
							</u-form-item>

							<u-form-item label="国际质量限" prop="gQualityLimit" label-width="180" required class="form-item">
								<shzy-select v-model="dataForm.gQualityLimit" placeholder="请选择"
									:options="gQualityLimitList"></shzy-select>
							</u-form-item>

						</view>

						<!-- 固定抽样 / 执行时自行设置-->
						<view v-show="dataForm.samplingType == '2' || dataForm.samplingType == '5'">

							<u-form-item label="抽样数量" prop="fixedSamplingQty" label-width="180" required class="form-item">
								<u-input v-model="dataForm.fixedSamplingQty" placeholder="0"></u-input>
							</u-form-item>

						</view>

						<!-- 固定比例 -->
						<view v-show="dataForm.samplingType == '3'">
							<u-form-item label="抽样比例" prop="fixedRatio" label-width="180" required class="form-item">
								<u-input v-model="dataForm.fixedRatio" placeholder="0"></u-input>
							</u-form-item>

						</view>

						<!-- 自定义AQL -->
						<view v-show="dataForm.samplingType == '4'">
							<u-form-item label="自定义AQL" prop="aQL" label-width="180" required class="form-item">
								<shzy-select v-model="dataForm.aQL" placeholder="请选择"
									:options="aQLList"></shzy-select>
							</u-form-item>

						</view>

					</view>
				</template>
				<u-gap v-show="param.sample == 'Y'" height="20" bg-color="#f0f2f6"></u-gap><!-- 间隔槽 -->

				<!-- 检验项目 -->
				<template>
					<view class="form-list">

						<!-- 检验项目 -->
						<u-form-item label="检验项目" prop="innspectionCode" label-width="180" required class="form-item">
							<shzy-select v-model="dataForm.innspectionCode" placeholder="请选择" :options="innspectionCodeList" lazy
								:props="{label:'innspectionName',value:'id'}" @change="innspectionCodeChange"></shzy-select>
						</u-form-item>

						<!-- 检验条目 -->
						<u-form-item label="检验条目" prop="innspectionEntry" label-width="180" required class="form-item">
							<shzy-select v-model="dataForm.innspectionEntry" placeholder="请选择" lazy
								:options="innspectionEntryList" :props="{label:'innspectionName',value:'id'}"></shzy-select>
						</u-form-item>

						<!-- 检验方法 -->
						<u-form-item label="检验方法" prop="innspectionMethod" label-width="180" required class="form-item">
							<shzy-select v-model="dataForm.innspectionMethod" placeholder="请选择" :options="innspectionMethodList"
								lazy :props="{label:'name',value:'id'}"></shzy-select>
						</u-form-item>

						<!-- 类型 -->
						<u-form-item label="类型" prop="type" label-width="180" required class="form-item">
							<u-radio-group v-model="dataForm.type" @change="typeChange">
								<u-radio v-for="(item,index) in typeList" :key="index" :name="item.name"
									:disabled="item.disabled">
									{{item.name}}
								</u-radio>
							</u-radio-group>
						</u-form-item>

						<view v-show="disabledType">
							<!-- 定量标准 -->
							<u-form-item label="定量标准" prop="quantitativeStandards" label-width="180" required class="form-item">
								<shzy-select v-model="dataForm.quantitativeStandards" placeholder="请选择"
									:options="quantitativeStandardsList" @change="rationChange"></shzy-select>
							</u-form-item>

							<!-- 标准值 -->
							<u-form-item label="标准值" prop="standardValue" label-width="180" required class="form-item">
								<u-input v-model="dataForm.standardValue" placeholder="0"></u-input>
							</u-form-item>

							<!-- 区间 prop会影响动态校验-->
							<u-form-item v-show="dataForm.quantitativeStandards == '1'" label="区间" prop="section" label-width="180"
								required class="form-item">
							
								<u-input :border="border" v-model="dataForm.minimumValue" placeholder="最小值"
									style="margin-right: 20rpx;"></u-input>
								<u-input :border="border" v-model="dataForm.maximumValue" placeholder="最大值"></u-input>
							</u-form-item>
							
							
							<!-- 允差 -->
							<u-form-item v-show="dataForm.quantitativeStandards == '7'" label="允差" prop="deviation"
								label-width="180" required class="form-item">
								<u-input :border="border" v-model="dataForm.lowerDeviation" placeholder="下偏差"
									style="margin-right: 20rpx;"></u-input>
								<u-input :border="border" v-model="dataForm.upperDeviation" placeholder="上偏差"></u-input>
							</u-form-item>

							<!-- 精度 -->
							<u-form-item label="精度" prop="accuracy" label-width="180" required class="form-item">
								<u-input v-model="dataForm.accuracy" placeholder="0"></u-input>
							</u-form-item>

							<!-- 单位 -->
							<u-form-item label="单位" prop="unit" label-width="180" required class="form-item">
								<shzy-select v-model="dataForm.unit" placeholder="请选择"
									:props="{label:'fullName',value:'fullName'}" :options="unitList"></shzy-select>
							</u-form-item>
						</view>

						<!-- 参考 -->
						<u-form-item label="参考" prop="referTo" label-width="180" class="form-item">
							<u-switch v-model="dataForm.referTo" ></u-switch>
						</u-form-item>

						<!-- 破坏性 -->
						<u-form-item label="破坏性" prop="destructiveness" label-width="180" class="form-item">
							<u-switch v-model="dataForm.destructiveness"></u-switch>
						</u-form-item>

						<!-- 备注 -->
						<u-form-item label="备注" prop="remarks" label-width="180" class="form-item">
							<u-input v-model="dataForm.remarks" placeholder=""></u-input>
						</u-form-item>

					</view>
				</template>
			</u-form>

		</view>

		<!-- 按钮 -->
		<view class="buttom-actions-addsample">
			<u-button class="buttom-btn" type="primary" @click="confirm">确定</u-button>
		</view>

	</view>
</template>

<script>
	import {
		getDictionaryDataSelector
	} from '@/api/common.js'
	import {
		GetInspectionPro,
		GetInspectionItem,
		GetInspectionMethod,
		SubmitsamplingType
	} from '@/api/q1/business/inspection/inspection.js'

	export default {
		
		data() {
			return {
				dataForm: {
					// 抽样方案
					samplingType: "", // 抽样方案
					// 国标AQL
					gLevel: "", // 检验水平
					gStringency: "", // 严格度
					gQualityLimit: "", // 质量限
					// 自定义AQL
					aQL: "", // 自定义

					fixedSamplingQty: "", // 固定抽样/执行时自行设置
					fixedRatio: 0, // 固定比例

					// 检验项目
					innspectionCode: '', // 检验项目
					innspectionEntry: '', // 检验条目
					innspectionMethod: '', // 检验方法
					type: '定量', // 类型
					quantitativeStandards: '', // 定量标准
					unit: '', // 单位
					referTo: false, // 参考
					destructiveness: false, // 破坏性
					remarks: '', // 备注
					standardValue: '0', // 标准值
					accuracy: "0", // 精度

					minimumValue: "0", // 区间最小值
					maximumValue: "0", // 区间最大值
					lowerDeviation: 0, // 允差下偏差
					upperDeviation: 0 // 允差上偏差
				},
				rules: {
					samplingType: [{
						required: false,
						message: '请选择抽样方案',
						trigger: 'blur',
					}],
					innspectionCode: [{
						required: true,
						message: '请选择检验项目',
						trigger: 'blur',
					}],
					innspectionEntry: [{
						required: true,
						message: '请选择检验条目',
						trigger: 'blur',
					}],
					innspectionMethod: [{
						required: true,
						message: '请选择检验方法',
						trigger: 'blur',
					}],
					gLevel: [{
						required: false,
						message: '请选择检验水平',
						trigger: 'blur',
					}],
					gStringency: [{
						required: false,
						message: '请选择国际严格度',
						trigger: 'blur',
					}],
					gQualityLimit: [{
						required: false,
						message: '请选择国际质量限',
						trigger: 'blur',
					}],
					aQL: [{
						required: false,
						message: '请选择自定义AQL',
						trigger: 'blur',
					}],
					quantitativeStandards: [{
						required: true,
						message: '请选择定量标准',
						trigger: 'blur',
					}],
					unit: [{
						required: true,
						message: '请选择单位',
						trigger: 'blur',
					}],
					standardValue: [{
						required: true,
						message: '请填写标准值',
						trigger: 'blur',
					}],
					accuracy: [{
						required: true,
						message: '请填写精度',
						trigger: 'blur',
					}],
					
					fixedSamplingQty: [{
						required: false,
						message: '请填写抽样数量',
						trigger: 'blur',
					}],
				},
				// 列选择器
				samplingTypeList: [], // 抽样方案
				gLevelList: [], // 检验水平
				gStringencyList: [], // 严格度
				gQualityLimitList: [], // 质量限
				aQLList: [], // 自定义AQL
				unitList: [], // 单位
				quantitativeStandardsList: [], // 定量标准
				innspectionCodeList: [], // 检验项目
				innspectionEntryList: [], // 检验条目
				innspectionEntryListFilter: [], // 检验条目
				innspectionMethodList: [], // 检验方法

				typeList: [{
						name: '定性',
						disabled: false
					},
					{
						name: '定量',
						disabled: false
					}
				],
				border: true,
				disabledType: true, // 定量显示，定性不显示
				selectShow: false, // 下拉框显示
				param:{
					code:'',// 检验批号
					sample:'',// 是否携带抽样方案新增['Y'、'N']
					sampleId:'',	// 抽样方案id，新增抽样方案没有id，在抽样方案下根据抽样方案id增加
					samplingQty:'',
					receivedQty:'',
					rejectionQty:'',
					incomingQty:''
				}
			}
		},
		onReady() {
			this.$refs.dataForm.setRules(this.rules); // 设置校验规则
		},
		watch: {
			
			"param.sample"(value){
				this.$set(this.rules.samplingType[0],'required',value == 'Y')
			},
			
			// 根据抽样方案动态校验
			"dataForm.samplingType"(value) {
				//this.$set(对象,字段,值) 例如：改变message: this.$set(this.rules.gLevel[0],'message', value == 1 ? "示例1" : "示例2")
				this.$set(this.rules.gLevel[0], "required", value == 1) // 检验水平
				this.$set(this.rules.gStringency[0], "required", value == 1) // 国际严格度
				this.$set(this.rules.gQualityLimit[0], "required", value == 1) // 国际质量限
				this.$set(this.rules.fixedSamplingQty[0], "required", value == 2 || value == 5) // 固定抽样
				this.$set(this.rules.aQL[0], "required", value == 4) // 自定义AQL
			},
			// 根据类型动态校验
			"dataForm.type"(value) {
				this.$set(this.rules.quantitativeStandards[0], "required", value == '定量') // 定量标准
				this.$set(this.rules.unit[0], "required", value == '定量') // 单位
				this.$set(this.rules.standardValue[0], "required", value == '定量') // 标准值
				this.$set(this.rules.accuracy[0], "required", value == '定量') // 精度
			},
			
		},	
		
		onLoad(option) {
		
			const json = JSON.parse(option.params)
	
			// 从json对象中获取参数
			this.param.code = json.code != '' ? json.code : '';		// 检验批号
			this.param.sample = json.sample != '' ? json.sample : '';	// 是否携带抽样方案新增
			this.param.sampleId = json.sampleId;
			this.param.samplingQty = json.samplingQty;
			this.param.receivedQty = json.receivedQty;
			this.param.rejectionQty = json.rejectionQty;
			this.param.incomingQty = json.incomingQty;
		},
		mounted() {

			// 加载下拉框
			this.getSelect("450528765840197893"); // 抽样方案
			this.getSelect("452364825939418949"); // 检验水平
			this.getSelect("452365702502817605"); // 严格度
			this.getSelect("452365990030745413"); // 质量限
			this.getSelect("451284871961123589"); // 自定义AQL
			this.getSelect("450959252454905413"); // 单位
			this.getSelect("452323604902715205"); // 定量标准

			this.getInspect(); // 加载检验项目
		},
		// beforeDestroy() {
		// 	// 移除滚动事件监听
		// 	window.removeEventListener('scroll', this.handleScroll);
		// },
		methods: {
			
			// 监听定量标准清空区间、允差
			rationChange(e){
				if(e != '1'){
					this.dataForm.minimumValue = '0';
					this.dataForm.maximumValue = '0';
				}else if(e!='7'){
					this.dataForm.lowerDeviation = '0';
					this.dataForm.upperDeviation = '0';
				}
			},

			// 监听检验项目改变检验条目
			innspectionCodeChange(e) {

				this.dataForm.innspectionEntry = ""; // 检验项目改变时情况检验条目
				const list = this.innspectionEntryListFilter;
				this.innspectionEntryList = [];
				for (let i = 0; i < list.length; i++) {
					if (list[i].ItemsId == e) {
						this.innspectionEntryList.push(list[i]);
					}
				}

				if (this.innspectionEntryList.length == 0) {
					this.$refs.uToast.show({
						title: '该检验项目无检验条目',
						type: 'default'
					})
				}
			},

			// 加载检验项目
			getInspect() {
				// 检验项目
				GetInspectionPro().then(res => {
					if (res.code == 200) {
						this.innspectionCodeList = res.data.list
						
					}
				}).catch((err) => {
					this.$refs.uToast.show({
						title: '异常:' + err,
						type: 'error'
					})
				});

				// 检验条目
				GetInspectionItem().then(res => {
					if (res.code == 200) {

						// this.innspectionEntryList = res.data.list
						this.innspectionEntryListFilter = res.data.list
					}
				}).catch((err) => {
					this.$refs.uToast.show({
						title: '异常:' + err,
						type: 'error'
					})
				});

				// 检验方法
				GetInspectionMethod().then(res => {
					if (res.code == 200) {

						this.innspectionMethodList = res.data.list
					}
				}).catch((err) => {
					this.$refs.uToast.show({
						title: '异常:' + err,
						type: 'error'
					})
				});
			},

			// 选择类型
			typeChange(e) {
				if (e == '定量') {
					// 定量禁用定量标准、标准值
					this.disabledType = true
				} else {
					this.disabledType = false
				}
			},

			// 加载数据字典下拉框
			getSelect(dictionaryTypeId) {
				getDictionaryDataSelector(dictionaryTypeId).then(res => {
					
					if (res.code == 200) {

						const list = res.data.list.map(item => {
							return {
								...item,
								id: item.enCode, // 字典项配置的编号
								label: item.fullName // 要显示的数据	
							}
						})

						if (dictionaryTypeId == '450528765840197893') this.samplingTypeList = list; // 抽样方案
						if (dictionaryTypeId == '452364825939418949') this.gLevelList = list; // 检验水平
						if (dictionaryTypeId == '452365702502817605') this.gStringencyList = list; // 严格度
						if (dictionaryTypeId == '452365990030745413') this.gQualityLimitList = list; // 质量限
						if (dictionaryTypeId == '451284871961123589') this.aQLList = list; // 自定义AQL
						if (dictionaryTypeId == "450959252454905413") this.unitList = list; // 单位
						if (dictionaryTypeId == "452323604902715205") this.quantitativeStandardsList = list; // 定量标准
					}

				}).catch((err) => {
					this.$refs.uToast.show({
						title: '异常:' + err,
						type: 'error'
					})
				});
			},
			
			// 转换
			transitionData(){
	
				var detailtype = this.dataForm.type == '定性' ? "qualitative" : 'ration';
				var detailreferTo = this.dataForm.referTo == true ? 'Y' : 'N';
				var detaildestructiveness = this.dataForm.destructiveness == true ? 'Y' : 'N';

				var array = [{
					// 检验项目
					innspectionCode: this.dataForm.innspectionCode, // 检验项目
					innspectionEntry: this.dataForm.innspectionEntry, // 检验条目
					innspectionMethod: this.dataForm.innspectionMethod, // 检验方法
					type: detailtype, // 类型
					quantitativeStandards: this.dataForm.quantitativeStandards, // 定量标准
					unit: this.dataForm.unit, // 单位
					referTo: detailreferTo, // 参考
					destructiveness: detaildestructiveness, // 破坏性
					remarks: this.dataForm.remarks, // 备注
					standardValue: this.dataForm.standardValue, // 标准值
					accuracy: this.dataForm.accuracy, // 精度
					fileInfo: [],
					minimumValue: this.dataForm.minimumValue, // 区间最小值
					maximumValue: this.dataForm.maximumValue, // 区间最大值
					lowerDeviation: this.dataForm.lowerDeviation, // 允差下偏差
					upperDeviation: this.dataForm.upperDeviation // 允差上偏差
				}]
				
				const params = {
					
					samplingType: this.dataForm.samplingType, // 抽样方案
					gLevel: this.dataForm.gLevel, // 检验水平
					gStringency: this.dataForm.gStringency, // 严格度
					gQualityLimit: this.dataForm.gQualityLimit, // 质量限
					aQL: this.dataForm.aQL, // 自定义
					fixedSamplingQty: this.dataForm.fixedSamplingQty, // 固定抽样/执行时自行设置
					fixedRatio: this.dataForm.fixedRatio, // 固定比例
					
					code:this.param.code,// 检验批号
					sampleId:this.param.sampleId,	// 抽样方案id，新增抽样方案没有id，在抽样方案下根据抽样方案id增加
					incomingQty:this.param.incomingQty,
					samplingQty:this.param.samplingQty,
					receivedQty:this.param.receivedQty,
					rejectionQty:this.param.rejectionQty,
					detailData:array
				}
				return params;
			},

			// 确定
			confirm() {
				this.$refs.dataForm.validate(valid => {
					if (valid) {
						// 校验区间、允差
						if(this.dataForm.type == '定量'){
							var hint = '';
							
							if(this.dataForm.quantitativeStandards == '1'){
								if(this.dataForm.minimumValue == ''){
									hint = '请填写区间最小值';
								}else if(this.dataForm.maximumValue == ''){
									hint = '请填写区间最大值';
								}
								
							}else if(this.dataForm.quantitativeStandards == '7'){
								if(this.dataForm.lowerDeviation == ''){
									hint = '请填写允差下偏差';
								}else if(this.dataForm.upperDeviation == ''){
									hint = '请填写允差上偏差';
								}
							}else if(this.dataForm.samplingType == 3 && this.dataForm.fixedRatio == ''){
								hint = '请填写抽样比例';
							}
							if(hint != ""){
								this.$refs.uToast.show({
									title: hint,
									type: 'default',
									duration: 1000
								});
								return;
							}
						}
						var data = this.transitionData();// 处理数据
						console.log(data)
						SubmitsamplingType(data).then(res => {
							if (res.code == 200 && (res.data == null || res.data == "")) {
						
								this.$refs.uToast.show({
									title: "提交成功",
									type: 'success',
									duration: 1000,
									callback() {
										uni.navigateBack(); // 返回上一页
									}
								});
							}else{
								this.$refs.uToast.show({
									title: "错误:" + res.data,
									type: 'error',
									duration: 1000,
								});
							}
						
						}).catch((err) => {
							console.log('调试接口异常:' + err)
							// this.$refs.uToast.show({
							// 	title: '异常:' + err,
							// 	type: 'error'
							// })
						}); 
						
					}else{
						console.log('false')
					}
				});

			}

		}
	}
</script>

<style>
	.form-container {
		padding-bottom: 100rpx;
	}

	.form-item {
		margin: 0 40rpx 0 40rpx;
	}

	.buttom-actions-addsample {
		background-color: #fff;
		position: fixed;
		bottom: 0;
		width: 100%;
		height: 44px;
		box-shadow: 0 -1px 4px #e1e5ec;
		z-index: 20;

		.buttom-btn {
			width: 100%;
			/* #ifndef MP */
			height: 88rpx !important;
			line-height: 88rpx !important;
			border-radius: 0 !important;

			&::after {
				border: none !important;
			}

			/* #endif */
			/* #ifdef MP */
			.u-btn {
				width: 100%;
				height: 88rpx !important;
				line-height: 88rpx !important;
				border-radius: 0 !important;

				&::after {
					border: none !important;
				}
			}

			/* #endif */
		}
	}
</style>